package zhongbo.java.air.flightpath.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.common.exception.MyException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.flightpath.entity.AirFlightPath;
import zhongbo.java.air.flightpath.listener.RedisLogChanelPublisher;
import zhongbo.java.air.flightpath.service.AirFlightPathService;

@RestController
@Api(tags = "航迹数据接口")
@RequestMapping("/flightPath")
public class AirFlightPathController {
    @Autowired
    private AirFlightPathService airFlightPathService;
    @Autowired
    private RedisLogChanelPublisher redisLogChanelPublisher;

    @PostMapping("/add")
    @ApiOperation("增加航迹记录")
    public ResultVo<AirFlightPath> addAirFlightPath(@RequestBody AirFlightPath airFlightPath) {
        try {
            AirFlightPath airFlightPath1 = airFlightPathService.addAirFlightPath(airFlightPath);
            redisLogChanelPublisher.broadcast("航迹记录添加成功");
            return ResultVo.ok(airFlightPath1);
        } catch (Exception e) {
            redisLogChanelPublisher.broadcast(e.getMessage());
            return ResultVo.error(e.getMessage());
        }

    }

    @GetMapping("/delete")
    @ApiOperation("删除航迹记录")
    public ResultVo<String> deleteAirFlightPath(@RequestParam String icaoCode) {
        try {
            airFlightPathService.deleteAirFlightPath(icaoCode);
            redisLogChanelPublisher.broadcast("航迹记录删除成功");
            return ResultVo.ok("1");
        } catch (MyException e) {
            redisLogChanelPublisher.broadcast(e.getMessage());
            return ResultVo.error(e.getMessage());
        }
    }

    //    @PostMapping("/updateAirFlightPath")
//    @ApiOperation("更新航迹记录")
//    public ResultVo<AirFlightPath> updateAirFlightPath(@RequestBody AirFlightPath airFlightPath) {
//        try {
//            airFlightPathService.updateAirFlightPath(airFlightPath);
//            return ResultVo.success();
//        } catch (MyException e) {
//            return ResultVo.error(e.getMessage());
//        }
//    }
    @PostMapping("/edit")
    @ApiOperation("更新航迹记录")
    public ResultVo<AirFlightPath> updateAirFlightPath(@RequestBody AirFlightPath airFlightPath) {
        try {
            AirFlightPath airFlightPath1 = airFlightPathService.updatePath(airFlightPath);
            redisLogChanelPublisher.broadcast("航迹记录更新成功");
            return ResultVo.ok(airFlightPath1);
        } catch (MyException e) {
            redisLogChanelPublisher.broadcast(e.getMessage());
            return ResultVo.error(e.getMessage());
        }
    }

    //    @PostMapping("/selectAirFlightPath")
//    @ApiOperation("查询航迹记录")
//    public ResultVo<AirFlightPath> selectAirFlightPath(@RequestBody AirFlightPath airFlightPath) {
//        try {
//            AirFlightPath airFlightPath1 = airFlightPathService.selectAirFlightPath(airFlightPath);
//            return ResultVo.ok(airFlightPath1);
//        } catch (MyException e) {
//            return ResultVo.error(e.getMessage());
//        }
//    }
    @GetMapping("/findOne")
    @ApiOperation("查询一条")
    public ResultVo<AirFlightPath> selectAirFlightPath(@RequestParam String icaoCode) {
        try {
            AirFlightPath airFlightPath1 = airFlightPathService.SelectByIcaoCode(icaoCode);
            redisLogChanelPublisher.broadcast("航迹记录查询成功:" + airFlightPath1);
            return ResultVo.ok(airFlightPath1);
        } catch (MyException e) {
            redisLogChanelPublisher.broadcast(e.getMessage());
            return ResultVo.error(e.getMessage());
        }
    }

    @GetMapping("findPage")
    @ApiOperation("分页查询列表")
    public ResultVo<PageVo<AirFlightPath>> page(Integer pageNumber, Integer pageSize, boolean order, String searchField, String keyword, String sortField) {
        PageVo<AirFlightPath> page = airFlightPathService.page(pageNumber, pageSize, order, searchField, keyword, sortField);
        return ResultVo.ok(page);
    }

    @GetMapping("pageByIcaoCode")
    @ApiOperation("分页查询2")
    public ResultVo<PageVo<AirFlightPath>> pageByIcaoCode(
            @RequestParam(defaultValue = "1") Integer pageNumber,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String icaoCode,
            @RequestParam(required = false)
            String startTime,
            @RequestParam(required = false)
            String endTime,
            @RequestParam(required = false)
            String oderByField,
            @RequestParam(required = false)
            boolean orderByAsc
    ) {

        try {
            PageVo<AirFlightPath> page = airFlightPathService.pageByIcaoCode(pageNumber, pageSize, icaoCode, startTime, endTime,oderByField,orderByAsc);
            return ResultVo.ok(page);
        } catch (MyException e) {
            return ResultVo.error(e.getMessage());
        }
    }

}
